<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<%@ taglib uri="/tags/core" prefix="c"%>
<script type="text/javascript">
	function validateForm(forma){
		var retVal = true;
		var total = document.getElementById("totalEtiquetado");
		if( parseFloat(total.value.replace(/,/g, "")) > parseFloat('<bean:write name="TagDistributionForm" property="tagDistributionView.purchaseOrderCapacity"/>'.replace(/,/g, "")) ){
			alert('<bean:message key="error.message.tagdistribution.capacity"/>');
			retVal = false;
		}
		return retVal;
	}
	function sumTotal(){
		var total = document.getElementById("totalEtiquetado");
		var sumTotal = 0
		var index = 0;
		var field;
		do{
			field = document.getElementById("tagQuantity"+index);
			if( field != undefined ){
				if(field.value == ""){
					field.value = "0.000";
				}
				sumTotal = sumTotal + parseFloat( field.value.replace(/,/g, "") );
			}
			index++;
		}while(field != undefined);
		total.value = addCommas( decimales( sumTotal, 3 ) );	
	}
</script>
<html:form action="tagDistributionSave">
	<table width="350" align="center" border="0">
		<tr>
			<td colspan="2" class="etiqueta_campo">
				<table width="100%" align="center" cellpadding="2" cellspacing="1" border="0" bgcolor="#CCCCC0">
					<tr>
						<th colspan="2" align="center" class="titulocolumna"><bean:message key="label.breadcrum.purchaseorder.version.tag"/></th>
					</tr>
					<tr>
						<td class="etiqueta" width="50%"><bean:message key="label.purchaseorder.name"/></td>
						<td class="etiqueta_campo_right" style="padding-right:25px;">
							<bean:write name="TagDistributionForm" property="tagDistributionView.purchaseOrderCode"/>
						</td>
					</tr>
					<tr>
						<td class="etiqueta" width="50%"><bean:message key="label.purchase.program.period"/></td>
						<td class="etiqueta_campo_right" style="padding-right:25px;">
							<bean:write name="TagDistributionForm" property="tagDistributionView.purchaseOrderBiweek"/>
						</td>
					</tr>
					<tr>
						<td class="etiqueta" width="50%"><bean:message key="label.purchaseorder.capacity"/></td>
						<td class="etiqueta_campo_right" style="padding-right:25px;">
							<input style="border: none;background-color: white;" type="text" name="capacidad" id="capacidad" class="columnanumero" size="15" disabled="disabled" value="<bean:write name="TagDistributionForm" property="tagDistributionView.purchaseOrderCapacity"/>"/>
						</td>
					</tr>
					<tr>
						<td class="etiqueta" width="50%"><bean:message key="label.purchaseorder.version.tagdistribution.total"/></td>
						<td class="etiqueta_campo_right" style="padding-right:25px;">
							<input style="border: none;background-color: white;"  type="text" name="totalEtiquetado" id="totalEtiquetado" class="columnanumero" size="15" disabled="disabled" value="0"/>
						</td>
					</tr>
				</table>
			</td>
		</tr>
	</table>
	<br/>
	<div style="width:975px; overflow-x:scroll;overflow:moz-scrollbars-horizontal" >
		<table style="width: 975px" align="center" cellpadding="2" cellspacing="1" border="0" bgcolor="#CCCCC0">
			<tr>
				<td colspan="<bean:write name="totalPeriods"/>" align="center" class="titulo_lista_interna"><B><bean:message key="label.breadcrum.plant.list"/></B></td>
			</tr>
			<tr>
				<td width="80" class="titulocolumna" style="text-align: center;"><bean:message key="label.plants.name"/></td>
				<logic:notEmpty name="periodosMensuales">
					<logic:iterate name="periodosMensuales"  id="periodos">
						<td width="100"  class="titulocolumna" style="text-align: center;">
							<bean:write name="periodos" property="listDescription"/>
						</td>
					</logic:iterate>
				</logic:notEmpty>
			</tr>
			<logic:present name="TagDistributionForm" property="tagDistributionView">
				<c:forEach items="${TagDistributionForm.tagDistributionView.tagPlantDistributionViews}" var="plants" varStatus="rowNumber">
					<tr class="columnadatoPar">
						<td style="text-align: center; vertical-align: top"><bean:write name="plants" property="plantView.shortName"/></td>
						<c:forEach items="${plants.tagPlantDistributionVersionViews}" var="tagPlantDistributionVersionView" varStatus="status">
							<td style="text-align: center; vertical-align: top">
								<input onchange="sumTotal();" type="text" class="columnanumero" id="tagQuantity<c:out value="${(rowNumber.index * ( totalPeriods - 1 ) ) + status.index}"/>" size="10" name="tagDistributionView.tagPlantDistributionViews[<c:out value="${rowNumber.index}"/>].tagPlantDistributionVersionViews[<c:out value="${status.index}"/>].quantity" value="<c:out value="${tagPlantDistributionVersionView.quantity}"/>" onfocus="this.select();" onkeydown="return (numeric(event, 1) && noPaste(event, this));" onkeyup="extractNumber(this,3,false);"/>
								<script type="text/javascript">
									var total = document.getElementById("totalEtiquetado");
									total.value = addCommas( decimales( parseFloat( total.value.replace(/,/g, "") ) + parseFloat( "<c:out value="${tagPlantDistributionVersionView.quantity}"/>".replace(/,/g, "") ), 3 )) ;
								</script>
							</td>
						</c:forEach>
					</tr>
				</c:forEach>
			</logic:present>
		</table>
	</div>
	<br/>
	<table align="center" width="350" cellpadding="2" cellspacing="1" border="0" bgcolor="#CCCCC0">
		<tr>
			<td style="text-align: center; vertical-align: bottom;" class="columnadatopar">
				<html:submit onclick="return validateForm(this.form);" styleClass="boton"><bean:message key="label.common.save"/></html:submit>
				<html:button property="cancel" onclick="location.href = 'showPurchaseOrderVersionsList.do'" styleClass="boton"><bean:message key="label.common.cancel"/></html:button>
			</td>
		</tr>
	</table>
</html:form>